<template>
  <div id="pictureManagePage">
    <a-flex justify="space-between">
      <h2>图片管理</h2>
      <a-space>
        <a-button type="primary" href="/addPicture" target="_blank">+ 创建图片</a-button>
        <a-button type="primary" href="/addPicture/batch" target="_blank" ghost>+ 批量创建图片</a-button>
      </a-space>
    </a-flex>
    <div style="margin-bottom: 16px" />
    <!-- 搜索表单 -->
    <a-form layout="inline" :model="searchParams" @finish="doSearch">
      <a-form-item label="关键词">
        <a-input
          v-model:value="searchParams.searchText"
          placeholder="从名称和简介搜索"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="类型">
        <a-input v-model:value="searchParams.category" placeholder="请输入类型" allow-clear />
      </a-form-item>
      <a-form-item label="标签">
        <a-select
          v-model:value="searchParams.tags"
          mode="tags"
          placeholder="请输入标签"
          style="min-width: 180px"
          allow-clear
        />
      </a-form-item>
      <a-form-item name="reviewStatus" label="审核状态">
        <a-select
          v-model:value="searchParams.reviewStatus"
          style="min-width: 180px"
          placeholder="请选择审核状态"
          :options="PIC_REVIEW_STATUS_OPTIONS"
          allow-clear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <div style="margin-bottom: 16px" />
    <!-- 表格 -->
    <a-table
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="doTableChange"
      :scroll="{ x: 'max-content' }"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'url'">
          <a-image :src="record.url" :width="120" />
        </template>
        <!-- 标签 -->
        <template v-if="column.dataIndex === 'tags'">
          <a-space wrap>
            <a-tag v-for="tag in JSON.parse(record.tags || '[]')" :key="tag">{{ tag }}</a-tag>
          </a-space>
        </template>
        <!-- 图片信息 -->
        <template v-if="column.dataIndex === 'picInfo'">
          <div>格式：{{ record.picFormat }}</div>
          <div>宽度：{{ record.picWidth }}</div>
          <div>高度：{{ record.picHeight }}</div>
          <div>宽高比：{{ record.picScale }}</div>
          <div>大小：{{ (record.picSize / 1024).toFixed(2) }}KB</div>
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.dataIndex === 'editTime'">
          {{ dayjs(record.editTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.key === 'action'">
          <a-space wrap>
            <a-button
              v-if="record.reviewStatus !== PIC_REVIEW_STATUS_ENUM.PASS"
              type="link"
              @click="handleReview(record, PIC_REVIEW_STATUS_ENUM.PASS)"
            >
              通过
            </a-button>
            <a-button
              v-if="record.reviewStatus !== PIC_REVIEW_STATUS_ENUM.REJECT"
              type="link"
              danger
              @click="showModel('是否设置为审核不通过？', 0, record)"
            >
              拒绝
            </a-button>
            <a-button type="link" :href="`/addPicture?id=${record.id}`" target="_blank">编辑</a-button>
            <a-button type="link" danger @click="showModel('是否删除图片？', 1, record)">删除</a-button>
          </a-space>
        </template>
        <!-- 类别 -->
        <template v-if="column.dataIndex === 'category'">
          <a-space wrap>
            <a-tag>{{ record.category }}</a-tag>
          </a-space>
        </template>
        <!-- 审核信息 -->
        <template v-if="column.dataIndex === 'reviewMessage'">
          <div>审核状态：{{ PIC_REVIEW_STATUS_MAP[record.reviewStatus] }}</div>
          <div>审核信息：{{ record.reviewMessage }}</div>
          <div>审核人：{{ record.reviewerId }}</div>
        </template>
      </template>
    </a-table>
  </div>
  <a-modal v-model:open="open" :title=modalTitle :confirm-loading="confirmLoading" @ok="handleOk" ok-text="确认" cancel-text="取消">
    <p>{{ modalText }}</p>
  </a-modal>
</template>


<script setup lang="ts">

import dayjs from 'dayjs'
import { deletePictureUsingPost, doPictureReviewUsingPost, listPictureByPageUsingPost } from '@/api/pictureController'
import { computed, onMounted, reactive, ref } from 'vue'
import { message } from 'ant-design-vue'
import { PIC_REVIEW_STATUS_ENUM, PIC_REVIEW_STATUS_MAP, PIC_REVIEW_STATUS_OPTIONS } from '@/constants/picture'

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '图片',
    dataIndex: 'url',
  },
  {
    title: '名称',
    dataIndex: 'name',
  },
  {
    title: '简介',
    dataIndex: 'introduction',
    ellipsis: true,
  },
  {
    title: '类型',
    dataIndex: 'category',
  },
  {
    title: '标签',
    dataIndex: 'tags',
  },
  {
    title: '图片信息',
    dataIndex: 'picInfo',
  },
  {
    title: '用户 id',
    dataIndex: 'userId',
    width: 80,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '编辑时间',
    dataIndex: 'editTime',
  },
  {
    title: '审核信息',
    dataIndex: 'reviewMessage',
  },
  {
    title: '操作',
    key: 'action',
  }
]
const confirmLoading = ref<boolean>(false)
const open = ref<boolean>(false)
const mode = ref<number>(0)
const modalTitle = ref<string>()
const modalText = ref<string>('')
const recordInfo = ref<API.Picture>()
// 数据
const dataList = ref([])
const total = ref(0)


// 搜索条件
const searchParams = reactive<API.PictureQueryRequest>({
  current: 1,
  pageSize: 10,
  sortField: 'createTime',
  sortOrder: 'descend',
})

// 按钮操作
const showModel = (text: string, m: number, record: API.Picture) => {
  open.value = true
  modalText.value = text
  mode.value = m
  recordInfo.value = record
  if (m === 0){
    modalTitle.value = '图片审核'
  }else {
    modalTitle.value = '删除图片'
  }
}
const handleOk = () => {
  console.log(mode.value)
  // mode:0 审核为拒绝,mode:1 删除图片信息
  if (mode.value === 0){
    confirmLoading.value = true
    handleReview(recordInfo.value, PIC_REVIEW_STATUS_ENUM.REJECT)
  }else if (mode.value === 1){
    confirmLoading.value = true
    doDelete(recordInfo.value.id)
  }
}

// 获取数据
const fetchData = async () => {
  const res = await listPictureByPageUsingPost({
    nullSpaceId: true,
    ...searchParams
  })
  if (res.data.code === 0 && res.data.data) {
    console.log(res.data.data)
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
}

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.current,
    pageSize: searchParams.pageSize,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total) => `共 ${total} 条`,
  }
})

// 表格变化之后，重新获取数据
const doTableChange = (page: never) => {
  searchParams.current = page.current
  searchParams.pageSize = page.pageSize
  fetchData()
}

// 搜索数据
const doSearch = () => {
  // 重置页码
  searchParams.current = 1
  fetchData()
}

// 删除数据
const doDelete = async (id: string) => {
  if (!id) {
    return
  }
  const res = await deletePictureUsingPost({ id })
  if (res.data.code === 0) {
    open.value = false
    message.success('删除成功')
    confirmLoading.value = false
    // 刷新数据
    await fetchData()
  } else {
    open.value = false
    message.error('删除失败')
    confirmLoading.value = false
  }
}

// 审核图片
const handleReview = async (record: API.Picture, reviewStatus: number) => {
  console.log(record)
  const reviewMessage = reviewStatus === PIC_REVIEW_STATUS_ENUM.PASS ? '管理员操作通过' : '管理员操作拒绝'
  const res = await doPictureReviewUsingPost({
    id: record.id,
    reviewStatus,
    reviewMessage,
  })
  if (res.data.code === 0) {
    open.value = false
    message.success('审核操作成功')
    confirmLoading.value = false
    // 重新获取列表
    await fetchData()
  } else {
    open.value = false
    message.error('审核操作失败，' + res.data.message)
    confirmLoading.value = false
  }
}


// 页面加载时获取数据，请求一次
onMounted(() => {
  fetchData()
})
</script >

<style scoped>

</style>
